/*
 * DateVerifier.java
 *
 * Created on September 14, 2005, 10:32 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ig.util;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author edjaiv
 */
public class DateVerifier extends InputVerifier {

    public boolean verify(JComponent input){
        boolean valid = true;        
        if (input instanceof JTextField) {            
            JTextField ftf = (JTextField)input;
            if(!ftf.getText().equals("")){
                SimpleDateFormat sdf = new SimpleDateFormat(LoadProperties.getProperties("server/configuracion.properties","formatoFechaPantalla"));
                sdf.setLenient(false);            
                valid = (sdf.parse(ftf.getText(),new ParsePosition(0)) != null);
            }else
                valid =true;
        }else if (input instanceof JFormattedTextField){            
            JFormattedTextField ftf = (JFormattedTextField)input;
            if(!ftf.getText().equals("")){            
                SimpleDateFormat sdf = new SimpleDateFormat(LoadProperties.getProperties("server/configuracion.properties","formatoFechaPantalla"));
                sdf.setLenient(false);
                valid = (sdf.parse(ftf.getText(),new ParsePosition(0)) != null);
            }else
                valid =true;            
        }            
        return valid;         
    }
    
    public boolean verify(String input){
        boolean valid = true;        
            if(!input.equals("")){
                SimpleDateFormat sdf = new SimpleDateFormat(LoadProperties.getProperties("server/configuracion.properties","formatoFechaPantalla"));
                sdf.setLenient(false);            
                valid = (sdf.parse(input,new ParsePosition(0)) != null);
            }else
                valid =true;
        return valid;                     
    }

        
    public boolean shouldYieldFocus(JComponent input) {
        boolean valid = verify(input);
        if(!valid){
            JOptionPane.showMessageDialog(input.getParent(),"La fecha no esta bien definida\nFormato de fecha:\n    ["
                +LoadProperties.getProperties("server/configuracion.properties","formatoFechaPantalla")+"]","Mensaje",JOptionPane.ERROR_MESSAGE);
        }
        return valid;
    }

}